package me.android.library.common.orm.ability;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import me.android.library.utils.android.ContentValuesUtil;
import mylab.base.foundation.lang.Identifiable;

public interface Update<T extends Identifiable<Long>> extends DS, EntityInfo<T> {

    default int updateById(T entity) {
        ContentValues content = ContentValuesUtil.fromBean(entity);
        String where = "id = ?";
        String[] args = new String[]{String.valueOf(entity.getId())};
        return update(content, where, args);
    }

    default int update(ContentValues content, String whereClause, String[] whereArgs) {
        EntityInfo.checkWhere(whereClause, whereArgs);
        try (SQLiteDatabase db = writableDb()) {
            return db.update(getTableName(), content, whereClause, whereArgs);
        }
    }
}
